// index.js
// 获取应用实例
const app = getApp()
const api = require('../../utils/api')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
  data: {
    gridList: [],//宫格列表
    bannerList: [],//banner列表
    addNewsList:[],//广告位
    tabbarList: [],//标签页列表
    findList: [],//找好玩
    jsxlList: [],//及食行乐
    tripList: [],//智能伴旅
    themeList: [],//品牌馆
    chosenList: [],//浙里优选
    sceneryList: [],//风物志
    playerList: [],//头号玩家
    scale: 1,//比例
    active: 0,//标签位置
    current: 0,//滑块位置
    bannerCurrent: 0,//首页大图位置
    bottomHeight: 0,//底部安全适配
    isSearch: true,//是否显示搜索框
    isdropDownOn: false,//是否开启下拉
    isScrollDown: false,//是否加载完了
    isScrollDownLoding: false,//是否开启底部loding事件
    scrollDownPage: 1,//瀑布流页码
    videoIndex: []//检测banner区视频
  },

  onLoad() {
    console.log(app.globalData);
    this.setData({
      navHeight: app.globalData.navHeight,
      tabHeight: app.globalData.tabHeight,
      bottomHeight: app.globalData.bottomHeight,
      scrollHeight: app.globalData.scrollHeight,
      scale: app.globalData.scale
    })
    if (wx.getStorageSync('pageNub') >= 0 && wx.getStorageSync('homeList')) {

      this.refreshPage(wx.getStorageSync('homeList'));
      /* 重进小程序刷新数据 */
      wx.removeStorageSync('pageNub');
      wx.removeStorageSync('homeList');
      this.getHomeInfoPage(0);
      /* 重进小程序刷新数据 */
    } else {
      this.getHomeInfoPage(0);
    }
  },

  onReady() {
    this.getTabBar().init();
    this.setData({
      videoCtx: wx.createVideoContext('myVideo', this)
    })
  },

  onShow() {
  },

  /* ---------------滚动标签函数--------------- */
  //滚动触发函数
  changeScroll(event) {
    let page = event.detail.scrollTop;
    if (page >= 100 && wx.getStorageSync('pageNub') == '0') {
      this.getHomeInfoPage(1);
    } else if (page >= 500 && wx.getStorageSync('pageNub') == '1') {
      this.getHomeInfoPage(2);
    } else if (page >= 1200 && wx.getStorageSync('pageNub') == '2') {
      this.getHomeInfoPage(3);
    } else if (page >= 1800 && wx.getStorageSync('pageNub') == '3') {
      this.getHomeInfoPage(4);
    }
  },

  //宫格滚动函数
  changeCurrent(event) {
    this.setData({
      current: event.detail.current
    })
  },

  //滚动banner触发
  onChangeBanner(e) {
    if (this.data.videoIndex != '') {
      wx.createVideoContext(this.data.videoIndex).pause();
      this.setData({ isSearch: true })
    }
    if (e.detail.currentItemId.indexOf('mp4') > -1) {
      wx.createVideoContext(e.detail.currentItemId).play();
      this.data.videoIndex = e.detail.currentItemId;
      this.setData({ isSearch: false })
    }
  },

  //下拉刷新
  dropDownOn() {
    try {
      api.getIsLogin({}).then(res => {
        app.globalData.loginAdmin = res.data;
        console.log(app.globalData.loginAdmin);
      })
      wx.removeStorageSync('pageNub');
      wx.removeStorageSync('homeList');
      wx.removeStorageSync('serviceList');
      wx.removeStorageSync('reserveList');
      this.data.isdropDownOn = true;
      this.getHomeInfoPage(0).then(() => {
        let time = setTimeout(() => {
          this.data.isdropDownOn = false;
          this.setData({
            isdropDownOn: false,
            isScrollDown: false,
            isScrollDownLoding: false,
            scrollDownPage: 1
          })
          clearTimeout(time);
        }, 500)
      })
    } catch (e) {
      console.log('缓存删除失败！');
    }
  },

  //滚动到底部时触发
  scrollDown() {

    if (this.data.isScrollDown || this.data.isScrollDownLoding || wx.getStorageSync('pageNub') != 4) return;
    this.setData({
      isScrollDownLoding: true,
      isScrollDown: false
    })
    this.data.scrollDownPage++;
    api.getHomeInfo({
      "loadPage": 4,
      "nowPage": this.data.scrollDownPage
    }).then((res) => {
      if (res.data.code === '1') return;
      let list = [];
      list = res.data.data[0].newsList;
      let time = setTimeout(() => {
        if (list.length == 0 || this.data.scrollDownPage >= 10) {
          this.setData({
            isScrollDownLoding: false,
            isScrollDown: true
          })
        } else {
          this.data.playerList.newsList = this.data.playerList.newsList.concat(list);
          this.setData({
            playerList: this.data.playerList,
            isScrollDownLoding: false
          })
        }
        clearTimeout(time);
      }, 300)
      //console.log(res);
    })
  },



  //获取首页信息
  getHomeInfoPage(page) {

    let nub = wx.getStorageSync('pageNub');
    if (page === nub) return;
    wx.setStorageSync('pageNub', page);
    return api.getHomeInfo({
      "loadPage": page,
      "tab": page == '2' ? '酒店' : ''
    }).then((res) => {
      //console.log(res);
      if (res.data.code === '1') return;
      let homeList = wx.getStorageSync('homeList');
      if (homeList) { } else homeList = [];
      homeList.push(...res.data.data);
      wx.setStorageSync('homeList', homeList);
      this.refreshPage(homeList);
    }).catch((ee) => {
      console.log(ee)
    })
  },




  //刷新渲染页面数据
  refreshPage(homeList) {
    return new Promise((resolve, reject) => {
      let bannerList = app.screenList(homeList, app.globalData.columnId.banner).newsList;//初始化banner列表
      let gridList = [];//初始化宫格
      let tabbarList = app.screenList(homeList, app.globalData.columnId.tabbar).newsList;//初始化底部标签
      let findList = [];//初始化找好玩
      let jsxlList = app.screenList(homeList, app.globalData.columnId.jsxl);//初始化及食行乐
      let tripList = app.screenList(homeList, app.globalData.columnId.trip);//初始化智能伴旅
      let themeList = app.screenList(homeList, app.globalData.columnId.theme);//初始化品牌馆
      let chosenList = app.screenList(homeList, app.globalData.columnId.chosen);//初始化浙里优选
      let sceneryList = app.screenList(homeList, app.globalData.columnId.scenery);//初始化风物志
      let playerList = app.screenList(homeList, app.globalData.columnId.player);//初始化头号玩家
      let bulletinList = app.screenList(homeList, app.globalData.columnId.bulletin);//初始化公告栏
      let hotNewsList = app.screenList(homeList, app.globalData.columnId.hotNews);//初始化热门资讯
      let addNewsList = app.screenList(homeList, app.globalData.columnId.addNews);//初始化广告位
      

      //宫格分页
      for (let i = 0; i < Math.ceil(app.screenList(homeList, app.globalData.columnId.grid).newsList.length / 10); i++) {
        gridList.push(app.screenList(homeList, app.globalData.columnId.grid).newsList.slice(i * 10, i * 10 + 10));
      }

      //找好玩组合
      findList = findList.concat(app.screenList(homeList, app.globalData.columnId.find))
        .concat(app.screenList(homeList, app.globalData.columnId.find1))
        .concat(app.screenList(homeList, app.globalData.columnId.find2))
        .concat(app.screenList(homeList, app.globalData.columnId.find3))
        .concat(app.screenList(homeList, app.globalData.columnId.find5))
        .concat(app.screenList(homeList, app.globalData.columnId.find4));


      //标签分页
      findList.forEach((i, index) => {
        app.spanSplit(findList[index]);
      })
      app.spanSplit(tripList);
      app.spanSplit(chosenList);

      //渲染操作
      this.getTabBar().init();
      //console.log(gridList);
      if (this.data.isdropDownOn) {

        this.setData({
          isdropDownOn: true,
          //bannerList: [],
          gridList: [],
          tabbarList: [],
          findList: [],
          jsxlList: [],
          tripList: [],
          themeList: [],
          chosenList: [],
          sceneryList: [],
          playerList: [],
          bulletinList: [],
          addNewsList: []
        });
      }
      this.setData({
        bannerList: bannerList,
        gridList: gridList,
        tabbarList: tabbarList,
        findList: findList,
        jsxlList: jsxlList,
        tripList: tripList,
        themeList: themeList,
        chosenList: chosenList,
        sceneryList: sceneryList,
        playerList: playerList,
        bulletinList: bulletinList,
        hotNewsList: hotNewsList,
        addNewsList: addNewsList,
        bannerCurrent: 0,//首页大图位置
      })
      resolve('ok')
    })

    //console.log(chosenList);
  },


  /* ---------------点击跳转函数--------------- */


  //点击跳转搜索
  onClickSearch(e) {
    if (app.openLoginPage()) return;
    app.toOutLink(e);
  },

  //浙里优选标签点击跳转
  changeTab(e) {
    api.getHomeInfo({
      "loadPage": 2,
      "tab": e.detail
    }).then((res) => {
      //console.log(res);
      let chosenList = res.data.data[0];
      app.spanSplit(chosenList);
      this.setData({
        chosenList: chosenList
      })
      //console.log(this.data.chosenList);
    })
  },

  //banner、宫格点击跳转外链
  onClickOutLink(e) {
    if (e.currentTarget.dataset.outLink == '1') {
      if (app.openLoginPage()) return;
    }
    app.toOutLink(e);
  },
  //点击打开公告弹窗
  onClickBulletin(msg) {
    Dialog.alert({
      context: this,
      message: msg.detail,
    })
  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index'
    }
  },
})
